//
//  yunPicListVC.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/11/24.
//  Copyright © 2015年 szb. All rights reserved.
//

#import "yunPicListVC.h"
#import "AppDelegate.h"
#import "QiniuSDK.h"
#import "BPhttpTool.h"
#import "MBProgressHUD+MJ.h"
#import "ZBCheckAlasset.h"
#import "AccountTool.h"

@interface yunPicListVC ()<ImageAddPreViewDelegate>

@property (nonatomic,strong)NSArray *yunArry;

@property (nonatomic,strong)AppDelegate *delegate;

@end

@implementation yunPicListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithButtonName:@"添加" withPic:@"buttonAdd" tatget:self action:@selector(addAlassets)];
    self.title = @"云相册";

    // Do any additional setup after loading the view.
}

//上传照片的按钮
- (void)addAlassets
{
    if (_picturePicker == nil) {
        _picturePicker = [[ZYQAssetPickerController alloc] init];
        _picturePicker.assetsFilter = [ALAssetsFilter allPhotos];
        _picturePicker.showEmptyGroups = NO;
    }
    
    _picturePicker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        if ([[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo]) {
            NSTimeInterval duration = [[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];
            return duration >= 5;
        } else {
            return YES;
        }
    }];
    //初始化底部照片栏
    [[_delegate preview] reMoveAllResource];
    [_delegate showPreView];
    //是否增加照片
    [_delegate preview].isAddPic = YES;
    //最多选择几张照片的提示文字
    [[_delegate preview] setUpYunPromptLabel:5];
    //设置最多选择照片的张数
    [_delegate preview].picnum = 5;
    //设置按钮
    [[_delegate preview].readyMakeBtn setTitle:@"上传照片" forState:UIControlStateNormal];
    _picturePicker.vc = self;
    [self presentViewController:_picturePicker animated:YES completion:nil];
    [_delegate preview].delegateSelectImage = self;
}
//上传照片
- (void)startPintuAction:(ImageAddPreView *)sender
{
    [self uploadAlasset];
}

- (void)uploadAlasset
{
    if (_delegate.yunIsPost == NO) {
        _delegate.yunIsPost = YES;
        _yunArry = [D_Main_Appdelegate preview].imageassets;
        ZBCheckAlasset *postAlasset = [[ZBCheckAlasset alloc] init];
        //这是我自写的方法，用来过滤重复的照片和已经上传过的照片，避免重复上传
        [postAlasset checkAlaaserArry:_yunArry withView:self.view];
        postAlasset.dismissBlock = ^(){
            //_delegate.backHome = YES;
            [self dismissViewControllerAnimated:YES completion:nil];
        };
        postAlasset.ZBblock = ^(){
            [self postYunPicture];
        };
    }else
    {
        [MBProgressHUD showError:@"正在上传请等待"];
    }
}

- (void)postYunPicture
{
    NSString *dir_id = [[NSUserDefaults standardUserDefaults] valueForKey:@"cloudFolderid"];
    NSMutableArray *arry = [NSMutableArray array];
    for (ALAsset *asset in _yunArry) {
        NSMutableDictionary *page = [NSMutableDictionary dictionary];
        NSString *key = [[asset defaultRepresentation].url absoluteString];
        NSRange range = [key rangeOfString:@"id="];
        key = [key substringFromIndex:range.location];
        key = [key stringByReplacingOccurrencesOfString:@"-" withString:@""];
        key = [key substringWithRange:NSMakeRange(3, 28)];
        
        [page setValue:dir_id forKey:@"dir_id"];
        [page setValue:key forKey:@"pic"];
        
        [BPhttpTool post:[NSString stringWithFormat:@"%@user/uploadimg",BPhttp] params:page success:^(id responseObj) {
            [arry addObject:responseObj];
            if (arry.count == _yunArry.count) {
                _delegate.yunIsPost = NO;
                [MBProgressHUD showSuccess:@"上传成功"];
                [self.context[@"loadimg"] callWithArguments:@[]];
                _block();
            }
            
        } failure:^(NSError *error) {
            _delegate.yunIsPost = NO;
        }];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
